{
 "cells": [
  {
   "cell_type": "code",
   "id": "initial_id",
   "metadata": {
    "collapsed": true,
    "ExecuteTime": {
     "end_time": "2025-01-08T11:32:00.288609Z",
     "start_time": "2025-01-08T11:31:59.718376Z"
    }
   },
   "source": "import akshare as ak",
   "outputs": [],
   "execution_count": 2
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-08T11:32:30.913516Z",
     "start_time": "2025-01-08T11:32:01.032751Z"
    }
   },
   "cell_type": "code",
   "source": [
    "stock_list = ak.stock_zh_a_spot()\n",
    "total_stocks = len(stock_list)"
   ],
   "id": "252a1830f3733b1a",
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "                                                                         \r"
     ]
    }
   ],
   "execution_count": 3
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-08T13:20:48.634644Z",
     "start_time": "2025-01-08T13:20:48.630660Z"
    }
   },
   "cell_type": "code",
   "source": "print(stock_list.columns)",
   "id": "68357c65f5f1b74e",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Index(['代码', '名称', '最新价', '涨跌额', '涨跌幅', '买入', '卖出', '昨收', '今开', '最高', '最低',\n",
      "       '成交量', '成交额', '时间戳'],\n",
      "      dtype='object')\n"
     ]
    }
   ],
   "execution_count": 10
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-08T13:21:30.902658Z",
     "start_time": "2025-01-08T13:21:30.893814Z"
    }
   },
   "cell_type": "code",
   "source": "print(stock_list.head())",
   "id": "74459f88807ff37a",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "         代码    名称    最新价   涨跌额    涨跌幅     买入     卖出     昨收     今开     最高  \\\n",
      "0  bj430017  星昊医药  14.69  0.29  2.014  14.68  14.69  14.40  14.20  15.10   \n",
      "1  bj430047  诺思兰德  11.85  0.09  0.765  11.83  11.85  11.76  11.80  12.17   \n",
      "2  bj430090  同辉信息   5.50  0.02  0.365   5.50   5.51   5.48   5.48   5.55   \n",
      "3  bj430139  华岭股份  23.00 -0.41 -1.751  23.00  23.01  23.41  22.81  23.67   \n",
      "4  bj430198  微创光电  10.47  0.08  0.770  10.47  10.48  10.39  10.39  10.52   \n",
      "\n",
      "      最低        成交量          成交额       时间戳  \n",
      "0  14.19  4540572.0   66672850.0  15:30:00  \n",
      "1  11.60  1677566.0   19798189.0  15:30:00  \n",
      "2   5.26  6807935.0   36951954.0  15:30:00  \n",
      "3  21.99  8481637.0  193323099.0  15:30:00  \n",
      "4   9.97  3614407.0   37190442.0  15:30:00  \n"
     ]
    }
   ],
   "execution_count": 11
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-08T13:22:49.010585Z",
     "start_time": "2025-01-08T13:22:48.946848Z"
    }
   },
   "cell_type": "code",
   "source": [
    "df = ak.stock_zh_a_hist(\n",
    "                symbol=\"bj430017\",\n",
    "                period=\"daily\",\n",
    "                start_date=\"20230101\",  # 5年前\n",
    "                end_date=\"20990101\",  # 当前日期\n",
    "                adjust=\"qfq\"  # 前复权数据\n",
    "            )\n",
    "\n",
    "# 保存数据\n",
    "df.to_csv(\"bj430017.csv\", index=False, encoding='utf-8-sig')"
   ],
   "id": "32ebdabaa2de9b02",
   "outputs": [
    {
     "ename": "KeyError",
     "evalue": "'bj430017'",
     "output_type": "error",
     "traceback": [
      "\u001B[1;31m---------------------------------------------------------------------------\u001B[0m",
      "\u001B[1;31mKeyError\u001B[0m                                  Traceback (most recent call last)",
      "Cell \u001B[1;32mIn[12], line 1\u001B[0m\n\u001B[1;32m----> 1\u001B[0m df \u001B[38;5;241m=\u001B[39m \u001B[43mak\u001B[49m\u001B[38;5;241;43m.\u001B[39;49m\u001B[43mstock_zh_a_hist\u001B[49m\u001B[43m(\u001B[49m\n\u001B[0;32m      2\u001B[0m \u001B[43m                \u001B[49m\u001B[43msymbol\u001B[49m\u001B[38;5;241;43m=\u001B[39;49m\u001B[38;5;124;43m\"\u001B[39;49m\u001B[38;5;124;43mbj430017\u001B[39;49m\u001B[38;5;124;43m\"\u001B[39;49m\u001B[43m,\u001B[49m\n\u001B[0;32m      3\u001B[0m \u001B[43m                \u001B[49m\u001B[43mperiod\u001B[49m\u001B[38;5;241;43m=\u001B[39;49m\u001B[38;5;124;43m\"\u001B[39;49m\u001B[38;5;124;43mdaily\u001B[39;49m\u001B[38;5;124;43m\"\u001B[39;49m\u001B[43m,\u001B[49m\n\u001B[0;32m      4\u001B[0m \u001B[43m                \u001B[49m\u001B[43mstart_date\u001B[49m\u001B[38;5;241;43m=\u001B[39;49m\u001B[38;5;124;43m\"\u001B[39;49m\u001B[38;5;124;43m20230101\u001B[39;49m\u001B[38;5;124;43m\"\u001B[39;49m\u001B[43m,\u001B[49m\u001B[43m  \u001B[49m\u001B[38;5;66;43;03m# 5年前\u001B[39;49;00m\n\u001B[0;32m      5\u001B[0m \u001B[43m                \u001B[49m\u001B[43mend_date\u001B[49m\u001B[38;5;241;43m=\u001B[39;49m\u001B[38;5;124;43m\"\u001B[39;49m\u001B[38;5;124;43m20990101\u001B[39;49m\u001B[38;5;124;43m\"\u001B[39;49m\u001B[43m,\u001B[49m\u001B[43m  \u001B[49m\u001B[38;5;66;43;03m# 当前日期\u001B[39;49;00m\n\u001B[0;32m      6\u001B[0m \u001B[43m                \u001B[49m\u001B[43madjust\u001B[49m\u001B[38;5;241;43m=\u001B[39;49m\u001B[38;5;124;43m\"\u001B[39;49m\u001B[38;5;124;43mqfq\u001B[39;49m\u001B[38;5;124;43m\"\u001B[39;49m\u001B[43m  \u001B[49m\u001B[38;5;66;43;03m# 前复权数据\u001B[39;49;00m\n\u001B[0;32m      7\u001B[0m \u001B[43m            \u001B[49m\u001B[43m)\u001B[49m\n\u001B[0;32m      9\u001B[0m \u001B[38;5;66;03m# 保存数据\u001B[39;00m\n\u001B[0;32m     10\u001B[0m df\u001B[38;5;241m.\u001B[39mto_csv(\u001B[38;5;124m\"\u001B[39m\u001B[38;5;124mbj430017.csv\u001B[39m\u001B[38;5;124m\"\u001B[39m, index\u001B[38;5;241m=\u001B[39m\u001B[38;5;28;01mFalse\u001B[39;00m, encoding\u001B[38;5;241m=\u001B[39m\u001B[38;5;124m'\u001B[39m\u001B[38;5;124mutf-8-sig\u001B[39m\u001B[38;5;124m'\u001B[39m)\n",
      "File \u001B[1;32mE:\\envs\\flask-shop-api\\Lib\\site-packages\\akshare\\stock_feature\\stock_hist_em.py:1051\u001B[0m, in \u001B[0;36mstock_zh_a_hist\u001B[1;34m(symbol, period, start_date, end_date, adjust, timeout)\u001B[0m\n\u001B[0;32m   1043\u001B[0m period_dict \u001B[38;5;241m=\u001B[39m {\u001B[38;5;124m\"\u001B[39m\u001B[38;5;124mdaily\u001B[39m\u001B[38;5;124m\"\u001B[39m: \u001B[38;5;124m\"\u001B[39m\u001B[38;5;124m101\u001B[39m\u001B[38;5;124m\"\u001B[39m, \u001B[38;5;124m\"\u001B[39m\u001B[38;5;124mweekly\u001B[39m\u001B[38;5;124m\"\u001B[39m: \u001B[38;5;124m\"\u001B[39m\u001B[38;5;124m102\u001B[39m\u001B[38;5;124m\"\u001B[39m, \u001B[38;5;124m\"\u001B[39m\u001B[38;5;124mmonthly\u001B[39m\u001B[38;5;124m\"\u001B[39m: \u001B[38;5;124m\"\u001B[39m\u001B[38;5;124m103\u001B[39m\u001B[38;5;124m\"\u001B[39m}\n\u001B[0;32m   1044\u001B[0m url \u001B[38;5;241m=\u001B[39m \u001B[38;5;124m\"\u001B[39m\u001B[38;5;124mhttps://push2his.eastmoney.com/api/qt/stock/kline/get\u001B[39m\u001B[38;5;124m\"\u001B[39m\n\u001B[0;32m   1045\u001B[0m params \u001B[38;5;241m=\u001B[39m {\n\u001B[0;32m   1046\u001B[0m     \u001B[38;5;124m\"\u001B[39m\u001B[38;5;124mfields1\u001B[39m\u001B[38;5;124m\"\u001B[39m: \u001B[38;5;124m\"\u001B[39m\u001B[38;5;124mf1,f2,f3,f4,f5,f6\u001B[39m\u001B[38;5;124m\"\u001B[39m,\n\u001B[0;32m   1047\u001B[0m     \u001B[38;5;124m\"\u001B[39m\u001B[38;5;124mfields2\u001B[39m\u001B[38;5;124m\"\u001B[39m: \u001B[38;5;124m\"\u001B[39m\u001B[38;5;124mf51,f52,f53,f54,f55,f56,f57,f58,f59,f60,f61,f116\u001B[39m\u001B[38;5;124m\"\u001B[39m,\n\u001B[0;32m   1048\u001B[0m     \u001B[38;5;124m\"\u001B[39m\u001B[38;5;124mut\u001B[39m\u001B[38;5;124m\"\u001B[39m: \u001B[38;5;124m\"\u001B[39m\u001B[38;5;124m7eea3edcaed734bea9cbfc24409ed989\u001B[39m\u001B[38;5;124m\"\u001B[39m,\n\u001B[0;32m   1049\u001B[0m     \u001B[38;5;124m\"\u001B[39m\u001B[38;5;124mklt\u001B[39m\u001B[38;5;124m\"\u001B[39m: period_dict[period],\n\u001B[0;32m   1050\u001B[0m     \u001B[38;5;124m\"\u001B[39m\u001B[38;5;124mfqt\u001B[39m\u001B[38;5;124m\"\u001B[39m: adjust_dict[adjust],\n\u001B[1;32m-> 1051\u001B[0m     \u001B[38;5;124m\"\u001B[39m\u001B[38;5;124msecid\u001B[39m\u001B[38;5;124m\"\u001B[39m: \u001B[38;5;124mf\u001B[39m\u001B[38;5;124m\"\u001B[39m\u001B[38;5;132;01m{\u001B[39;00m\u001B[43mcode_id_dict\u001B[49m\u001B[43m[\u001B[49m\u001B[43msymbol\u001B[49m\u001B[43m]\u001B[49m\u001B[38;5;132;01m}\u001B[39;00m\u001B[38;5;124m.\u001B[39m\u001B[38;5;132;01m{\u001B[39;00msymbol\u001B[38;5;132;01m}\u001B[39;00m\u001B[38;5;124m\"\u001B[39m,\n\u001B[0;32m   1052\u001B[0m     \u001B[38;5;124m\"\u001B[39m\u001B[38;5;124mbeg\u001B[39m\u001B[38;5;124m\"\u001B[39m: start_date,\n\u001B[0;32m   1053\u001B[0m     \u001B[38;5;124m\"\u001B[39m\u001B[38;5;124mend\u001B[39m\u001B[38;5;124m\"\u001B[39m: end_date,\n\u001B[0;32m   1054\u001B[0m     \u001B[38;5;124m\"\u001B[39m\u001B[38;5;124m_\u001B[39m\u001B[38;5;124m\"\u001B[39m: \u001B[38;5;124m\"\u001B[39m\u001B[38;5;124m1623766962675\u001B[39m\u001B[38;5;124m\"\u001B[39m,\n\u001B[0;32m   1055\u001B[0m }\n\u001B[0;32m   1056\u001B[0m r \u001B[38;5;241m=\u001B[39m requests\u001B[38;5;241m.\u001B[39mget(url, params\u001B[38;5;241m=\u001B[39mparams, timeout\u001B[38;5;241m=\u001B[39mtimeout)\n\u001B[0;32m   1057\u001B[0m data_json \u001B[38;5;241m=\u001B[39m r\u001B[38;5;241m.\u001B[39mjson()\n",
      "\u001B[1;31mKeyError\u001B[0m: 'bj430017'"
     ]
    }
   ],
   "execution_count": 12
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-08T09:12:31.596429Z",
     "start_time": "2025-01-08T09:12:31.593272Z"
    }
   },
   "cell_type": "code",
   "source": "",
   "id": "5ddb964cbaa73301",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'D:\\\\codes\\\\Pycharm\\\\flask-shop-api'"
      ]
     },
     "execution_count": 34,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 34
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
